﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Jewelnoid
{
    class LevelArcade : Level
    {
        public bool oldBallReleased;
        public int oldLifes;

        private Texture2D blockTexture;

        public override void start()
        {
            base.start();

            this.blockTexture = Game1.self.Content.Load<Texture2D>("Block");

            mapPhaseOne();
            this.blockManager.blocksStartPosition = 60; //Não entendi, mas ta centralizado em 60. Achei q devia ser em 40. De qualquer forma a fase vai dizer onde os blocos devem ficar.

            this.oldLifes = this.lvlInterface.lifes = 3;
            this.lvlInterface.update();

            this.oldBallReleased = false;

            SoundManager.playMusic("SpaceInvaders", true);
        }

        public override void terminate()
        {
            base.terminate();

            SoundManager.stopMusic();
        }

        public void mapPhaseOne()  //Tirar numeros mágicos depois
        {
            blockManager.blocksMatrix[0][2] = 1;
            blockManager.blocksMatrix[0][7] = 2;

            blockManager.blocksMatrix[1][3] = 3;
            blockManager.blocksMatrix[1][6] = 4;
            blockManager.blocksMatrix[2][2] = 1;
            blockManager.blocksMatrix[2][3] = 2;
            blockManager.blocksMatrix[2][4] = 3;
            blockManager.blocksMatrix[2][5] = 4;
            blockManager.blocksMatrix[2][6] = 4;
            blockManager.blocksMatrix[2][7] = 3;

            blockManager.blocksMatrix[3][1] = 2;
            blockManager.blocksMatrix[3][2] = 1;
            blockManager.blocksMatrix[3][4] = 2;
            blockManager.blocksMatrix[3][5] = 1;
            blockManager.blocksMatrix[3][7] = 4;
            blockManager.blocksMatrix[3][8] = 3;

            blockManager.blocksMatrix[4][0] = 3;
            blockManager.blocksMatrix[4][1] = 1;
            blockManager.blocksMatrix[4][2] = 4;
            blockManager.blocksMatrix[4][3] = 2;
            blockManager.blocksMatrix[4][4] = 2;
            blockManager.blocksMatrix[4][5] = 4;
            blockManager.blocksMatrix[4][6] = 1;
            blockManager.blocksMatrix[4][7] = 3;
            blockManager.blocksMatrix[4][8] = 2;
            blockManager.blocksMatrix[4][9] = 3;

            blockManager.blocksMatrix[5][0] = 1;
            blockManager.blocksMatrix[5][2] = 4;
            blockManager.blocksMatrix[5][3] = 1;
            blockManager.blocksMatrix[5][4] = 3;
            blockManager.blocksMatrix[5][5] = 4;
            blockManager.blocksMatrix[5][6] = 2;
            blockManager.blocksMatrix[5][7] = 1;
            blockManager.blocksMatrix[5][9] = 2;

            blockManager.blocksMatrix[6][0] = 3;
            blockManager.blocksMatrix[6][2] = 4;
            blockManager.blocksMatrix[6][7] = 3;
            blockManager.blocksMatrix[6][9] = 2;

            blockManager.blocksMatrix[7][3] = 2;
            blockManager.blocksMatrix[7][6] = 4;

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Vector2 unbreakableBlockPosition = new Vector2((this.windowSize.X / 5 + 5) * (j + 1) - blockTexture.Width / 2, 3 * this.windowSize.Y / 4 - blockTexture.Height / 2 + (blockTexture.Height + 2) * i);

                    UnbreakableBlock block = new UnbreakableBlock(this.blockTexture, unbreakableBlockPosition);
                    blockManager.unbreakableBlocksList.Add(block);
                }
            }
        }

        public override void update(GameTime time)
        {
            base.update(time);

            if (ballReleased == true && oldBallReleased == false)
            {
                SoundManager.playSound("shoot");
            }

            if (ballCollidedBlock)
            {
                SoundManager.playSound("invaderkilled");
            }

            if (oldLifes - 1 == lvlInterface.lifes)
            {
                SoundManager.playSound("explosion");
            }

            oldBallReleased = ballReleased;
            oldLifes = lvlInterface.lifes;

            checkEndPhase();
        }

        public void checkEndPhase()
        {
            if (lvlInterface.lifes == 0 || this.blockManager.isBlockMatrixEmpty())
            {
                SceneManager.setScene(new Menu());
            }
        }

    }
}
